home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1996 April / Software of the Month Club 1996 April.iso / pc / os2 / psutils / cmd / psmerge.cmd < prev    next >
OS/2 REXX Batch file  |  1996-02-21  |  2KB  |  88 lines

  1. extproc perl5 -x
  2. #! perl5
  3.  
  4. # psmerge: merge PostScript files produced by same application and setup
  5. # usage: psmerge [-oout.ps] [-thorough] file1.ps file2.ps ...
  6. #
  7. # Copyright (C) Angus J. C. Duggan 1991-1995
  8. # See file LICENSE for details.
  9.  
  10. $prog = ($0 =~ s=.*/==);
  11.  
  12. while ($ARGV[0] =~ /^-/) {
  13.    $_ = shift;
  14.    if (/^-o(.+)/) {
  15.       if (!close(STDOUT) || !open(STDOUT, ">$1")) {
  16.      print STDERR "$prog: can't open $1 for output\n";
  17.      exit 1;
  18.       }
  19.    } elsif (/^-t(horough)?$/) {
  20.       $thorough = 1;
  21.    } else {
  22.       print STDERR "Usage: $prog [-oout] [-thorough] file...\n";
  23.       exit 1;
  24.    }
  25. }
  26.  
  27. $page = 0;
  28. $first = 1;
  29. $nesting = 0;
  30.  
  31. @header = ();
  32. $header = 1;
  33.  
  34. @trailer = ();
  35. $trailer = 0;
  36.  
  37. @pages = ();
  38. @body = ();
  39.  
  40. @resources = ();
  41. $inresource = 0;
  42.  
  43. while (<>) {
  44.    if (/^%%BeginFont:/ || /^%%BeginResource:/ || /^%%BeginProcSet:/) {
  45.       $inresource = 1;
  46.       push(@resources, $_);
  47.    } elsif ($inresource) {
  48.       push(@resources, $_);
  49.       $inresource = 0 if /^%%EndFont/ || /^%%EndResource/ || /^%%EndProcSet/;
  50.    } elsif (/^%%Page:/ && $nesting == 0) {
  51.       $header = $trailer = 0;
  52.       push(@pages, join("", @body)) if @body;
  53.       $page++;
  54.       @body = ("%%Page: ($page) $page\n");
  55.    } elsif (/^%%Trailer/ && $nesting == 0) {
  56.       push(@trailer, $_);
  57.       push(@pages, join("", @body)) if @body;
  58.       @body = ();
  59.       $trailer = 1;
  60.       $header = 0;
  61.    } elsif ($header) {
  62.       push(@trailer, $_);
  63.       push(@pages, join("", @body)) if @body;
  64.       @body = ();
  65.       $trailer = 1;
  66.       $header = 0;
  67.    } elsif ($trailer) {
  68.       if (/^%!/ || /%%EOF/) {
  69.      $trailer = $first = 0;
  70.       } elsif ($first) {
  71.      push(@trailer, $_);
  72.       }
  73.    } elsif (/^%%BeginDocument/ || /^%%BeginBinary/ || /^%%BeginFile/) {
  74.       push(@body, $_);
  75.       $nesting++;
  76.    } elsif (/^%%EndDocument/ || /^%%EndBinary/ || /^%%EndFile/) {
  77.       push(@body, $_);
  78.       $nesting--;
  79.    } else {
  80.       print $_ if $print;
  81.    }
  82. }
  83.  
  84. print @trailer;
  85.  
  86. exit 0;
  87. # End of Script
  88.